package com.honey.mybatis.generator.entity.method;

import java.util.List;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.JavaKeyWord;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.MethodDecoration;
import com.honey.compilation.java.Parameter;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.dbmapping.structure.Column;
import com.honey.core.dbmapping.structure.Schema;
import com.honey.core.utils.JavaBeansUtil;
import com.honey.mybatis.generator.AbstractMethod;
import com.honey.mybatis.generator.MybatisPlugin;

public class BlobConstructorMethod extends AbstractMethod {
	
	private static final String [] JAVA_DOC = new String[]{
		"带参数构造函数。",
	};
	
	public BlobConstructorMethod(AbstractJavaCompilation clazz,
			MybatisPlugin belongPlugin, IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(clazz, belongPlugin, introspectSchema, introspectSchemaColumn);
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodDecoration()
	 */
	@Override
	protected MethodDecoration getMethodDecoration() {
		MethodDecoration answer = super.getMethodDecoration();
		answer.setConstructor(true);
		return answer;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getMethodName()
	 */
	@Override
	protected String getMethodName() {
		String answer = null ;
		answer = clazz.getType().getShortName();
		return answer ;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#getJavaDoc()
	 */
	@Override
	protected String[] getJavaDoc() {
		return JAVA_DOC;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#isBaseType()
	 */
	@Override
	protected boolean isBaseType() {
		return mybatisPlugin.getProjectEnvironmentHolder().isForcePrimitiveType();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractMethod#addBody(com.honey.compilation.java.Method)
	 */
	@Override
	protected void addBody(Method method) {
		List<Column> columns = introspectSchemaColumn.allColumns();
		Parameter parameter = null;
		Schema schema = introspectSchema.getSchema() ;
		for(Column column : columns ){
			parameter =new Parameter(
					super.getFieldType(schema, column),
					getFieldName(column));
			parameter.setComment(column.getComment());
			parameter.addJavaDocLine(column.getComment());
			method.addParameter(parameter);
			if(mybatisPlugin.isEntityTrimStrings()){
				String s = JavaKeyWord.OPERATION_THIS + JavaBeansUtil.getSetterMethodName(super.getMethodName(column))+
				"( " + parameter.getName() + " )"
				;
				
				method.addBodyLine(s);
				
			}else{
				method.addBodyLine(JavaKeyWord.OPERATION_THIS + parameter.getName() +" = " + parameter.getName());
			}
		}
	}
	
}
